<template>
    <div class="system-offer-container layout-padding">
        <el-card shadow="hover" class="layout-padding-auto">
            <el-card shadow="hover" class="layout-padding-auto">
                <div class="table-search-container">
                    <el-form ref="tableSearchRef" :model="state.ruleForm" size="default" label-width="100px"
                        class="table-form">
                        <el-row>
                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
                                <el-form-item label="账户地址">
                                    <el-input v-model="state.ruleForm.address" style="width: 100%"
                                        placeholder="账户地址"></el-input>
                                </el-form-item>
                            </el-col>





                            <!-- <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="系统地址">
                                    <el-select v-model="state.ruleForm.address" placeholder="系统地址" clearable
                                        style="width: 100%">
                                        <el-option v-for="i in state.systemAddressList" :key="i.address" :value="i.address"
                                            :label="i.addressTypeName + '[' + i.address + ']'">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col> -->


                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="状态">
                                    <el-select v-model="state.ruleForm.accountStatus" placeholder="请选择" clearable
                                        style="width: 100%">
                                        <el-option label="可用" value="10"></el-option>
                                        <el-option label="禁用" value="-10"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>




                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="币种">
                                    <el-select v-model="state.ruleForm.coinId" placeholder="请选择" clearable
                                        style="width: 100%">
                                        <el-option v-for="i in state.coinList" :key="i.coinId" :value="i.coinId"
                                            :label="i.coinSymbol">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>



                            <el-col :xs="24" :sm="12" :md="9" :lg="7" :xl="5" class="mb20" v-show="state.isToggle">
                                <el-form-item label="创建时间">
                                    <el-date-picker v-model="state.ruleForm.createDate" type="datetimerange"
                                        :picker-options="state.pickerOptions" range-separator="至" start-placeholder="开始日期"
                                        end-placeholder="结束日期" align="right" style="width: 100%">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>


                            <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                                <el-form-item class="table-form-btn" :label-width="!state.isToggle ? '10px' : '35px'">
                                    <div class="table-form-btn-toggle ml10" @click="state.isToggle = !state.isToggle">
                                        <span>{{ state.isToggle ? '收筛选' : '展筛选' }}</span>
                                        <SvgIcon :name="state.isToggle ? 'ele-ArrowUp' : 'ele-ArrowDown'" />
                                    </div>
                                    <div>
                                        <el-button size="default" type="primary" @click="refresh()">查询
                                        </el-button>
                                        <el-button size="default" type="info" class="ml10" @click="handleResetSearch()"> 重置
                                        </el-button>
                                    </div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>


                <el-table :default-sort="{ prop: 'CREATED_DATE', order: 'descending' }" size="default"
                    :data="state.dataList" style="width: 100%" border="" @sort-change="onSortHandler">

                    <el-table-column align="center" label="id" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.id }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="币种" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.coinSymbol }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="币种Id" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.coinId }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="用户地址" show-overflow-tooltip width="180">
                        <template #default="scope">
                            <span>{{ scope.row.address }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="余额" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.balanceAmount }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="冻结" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.freezeAmount }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="每日提现额度" width="160">
                        <template #default="scope">
                            <span>{{ scope.row.dailyTotalwithdrawalsAmount }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="累计收支">
                        <el-table-column align="center" label="累计收入" width="160">
                            <template #default="scope">
                                <span>{{ scope.row.incomeAmount || 0 }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column align="center" label="累计支出" width="160">
                            <template #default="scope">
                                <span>{{ scope.row.payAmount || 0 }}</span>
                            </template>
                        </el-table-column>
                    </el-table-column>

                    <el-table-column align="center" label="累计充提">
                        <el-table-column align="center" label="累计充值" width="160">
                            <template #default="scope">
                                <span>{{ scope.row.rechargeAmount || 0 }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column align="center" label="累计提现" width="160">
                            <template #default="scope">
                                <span>{{ scope.row.withdrawalsAmount || 0 }}</span>
                            </template>
                        </el-table-column>
                    </el-table-column>

                    <el-table-column align="center" label="后台累计充提">
                        <el-table-column align="center" label="累计充值" width="160">
                            <template #default="scope">
                                <span>{{ scope.row.backRechargeAmount || 0 }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column align="center" label="累计提现" width="160">
                            <template #default="scope">
                                <span>{{ scope.row.backWithdrawalsAmount || 0 }}</span>
                            </template>
                        </el-table-column>
                    </el-table-column>



                    <el-table-column align="center" label="状态">
                        <template #default="scope">
                            <el-tag v-if="scope.row.accountStatus == 10" effect="dark" type="success">可用</el-tag>
                            <el-tag v-if="scope.row.accountStatus == -10" effect="dark" type="danger">禁用</el-tag>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="创建时间" width="160">
                        <template #default="scope">
                            <span>{{ formatDate2(scope.row.createTime) }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="更新时间" width="160">
                        <template #default="scope">
                            <span>{{ formatDate2(scope.row.updateTime) }}</span>
                        </template>
                    </el-table-column>


                    <el-table-column fixed="right" align="center" label="操作" width="180">
                        <template #default="scope">
                            <div style="margin-top: 10px">
                                <el-button @click.native.prevent="
                                    openAdminUpdateRechargeWithdraw(scope.row)
                                    " size="small" type="primary">提现额度
                                </el-button>
                            </div>
                            <div style="margin-top: 10px">
                                <el-button @click.native.prevent="
                                    openAdminRechargeWithdraw(scope.row)
                                    " size="small" type="primary">充值提现
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>



                </el-table>
            </el-card>

            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                :pager-count="5" :page-sizes="[20, 50, 100]" v-model:current-page="state.page" background
                v-model:page-size="state.limit" layout="total, sizes, prev, pager, next, jumper" :total="state.total">
            </el-pagination>
        </el-card>

        <editdialogRW ref="editdialogRWRef" @refresh="refresh"></editdialogRW>
        <editDialog ref="editDialogRef" @refresh="refresh"></editDialog>
    </div>
</template>

<script setup lang="ts" name="systemMenu">
import { formatDate } from '/@/utils/formatTime';
import { onMounted, reactive, defineAsyncComponent, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';




import { useFundApi } from '/@/api/fund/index'



// 引入组件
const editDialog = defineAsyncComponent(() => import('./dialog.vue'));
const editDialogRef = ref();


// 引入组件
const editdialogRW = defineAsyncComponent(() => import('./editdialogRW.vue'));
const editdialogRWRef = ref();





const state = reactive({
    isToggle: false,
    sorts: {},
    page: 1,
    limit: 20,
    total: 0,
    dataList: [] as any,
    systemAddressList: [] as any,
    coinList: [] as any,
    systemTypeSelect: [] as any,// 系统账户类型
    ruleForm: {
        address: "",
        accountStatus: "",
        coinId: "",
        createDate: []
    },
    pickerOptions: {
        shortcuts: [
            {
                text: "最近一周",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 7
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
            {
                text: "最近一个月",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 30
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
            {
                text: "最近三个月",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 90
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
        ],
    }

});


const openAdminUpdateRechargeWithdraw = (row: any) => {
    editDialogRef.value.openDialog('edit', row);
}


const addItem = () => {
    editDialogRef.value.openDialog('add');
}

const refresh = () => {
    accountList();
}


const openAdminRechargeWithdraw = (row: any) => {
    editdialogRWRef.value.openDialog('edit', row);
}



const handleResetSearch = () => {
    state.ruleForm = {
        address: "",
        accountStatus: "",
        coinId: "",
        createDate: []
    };
    state.page = 1;
    state.limit = 20;
    accountList();
}


const formatDate2 = (str: any) => {
    if (!str) return "-";
    return formatDate(new Date(str), "YYYY-mm-dd HH:MM")
}


// 分页改变
const onHandleSizeChange = (val: number) => {
    state.limit = val;
    accountList();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
    state.page = val;
    accountList();
};

const getUserLevStr = (row: any) => {
    let str = row;
    state.systemTypeSelect.forEach((element: any) => {
        if (element.value == row) {
            str = element.name;
        }
    });
    return str;
}


const accountList = async () => {
    let vo = {
        page: state.page,
        limit: state.limit
    } as any;


    if (state.ruleForm.address) vo["address"] = state.ruleForm.address;
    if (state.ruleForm.accountStatus) vo["accountStatus"] = state.ruleForm.accountStatus;
    if (state.ruleForm.coinId) vo["coinId"] = state.ruleForm.coinId;
    let startTime, endTime;
    // if (state.ruleForm.createDate && state.ruleForm.createDate.length > 0) {
    //     startTime = state.ruleForm.createDate[0];
    //     endTime = state.ruleForm.createDate[1];
    // }
    // if (startTime) vo["startTime"] = startTime;
    // if (endTime) vo["endTime"] = endTime;


    if (state.ruleForm.createDate.length > 0) {
        vo["createTime"] = [formatDate(new Date(state.ruleForm.createDate[0]), "YYYY-mm-dd HH:MM:SS"), formatDate(new Date(state.ruleForm.createDate[1]), "YYYY-mm-dd HH:MM:SS")].join();
    }

    // if (sortsStr()) vo["sorts"] = sortsStr();


    let tempList = await useFundApi().accountList(vo);
    state.dataList = tempList.list;
    state.total = tempList.total;
}


const querySystemAddress = async () => {
    let listTemp = await useFundApi().querySystemAddress({ page: 1, limit: 200 });
    state.systemAddressList = listTemp.list;
}



const getcoinList = async () => {
    let listTemp = await useFundApi().getcoinList();
    state.coinList = listTemp;
}


const sortsStr = () => {
    let result = "";
    for (var prop in state.sorts) {
        result += prop + (state.sorts[prop] == "descending" ? " desc," : " asc,")
    }
    if (result == "") {
        return null;
    }
    return result.substring(0, result.length - 1)
}

const onSortHandler = (args: any) => {
    console.log(args)
    if (args.order) {
        state.sorts[args.prop] = args.order;
    } else {
        delete state.sorts[args.prop];
    }
    accountList();
}


// 页面加载时
onMounted(() => {
    // querySystemAddress();
    getcoinList();
    onSortHandler({ prop: "create_Time", order: "descending" });
});





</script>



<style scoped lang="scss">
.bar_tools {
    display: flex;
    flex-direction: row-reverse;
    margin-bottom: 10px;
}

.system-offer-container {
    :deep(.el-card__body) {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;

        .el-table {
            flex: 1;
        }
    }
}


.table-search-container {
    display: flex;

    .table-form {
        flex: 1;

        .table-form-btn-toggle {
            white-space: nowrap;
            user-select: none;
            display: flex;
            align-items: center;
            color: var(--el-color-primary);
            margin-right: 15px;
        }
    }
}
</style>

